package ufc.rafaelcds.combudroid.misc;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class FacebookUtil {
	
	private static final String APP_ID = "197386840320437";
	private static final String PREFERENCES_NAME = "CombuDroid";
	private static final String[] PERMISSOES = new String[] {"offline_access", "publish_stream"};
	
	public static final Facebook FACEBOOK = new Facebook(APP_ID);

	public static void publicar(final Activity activity, final String mensagem, final Runnable callback) {
		login(activity, new Runnable() {
			@Override
			public void run() {
				try {
					Bundle parametros = new Bundle();
					parametros.putString("message", mensagem);
					FACEBOOK.request("/me/feed", parametros, "POST");
				} catch (Exception e) {
					Log.d(FacebookUtil.class.getName(), "Erro ao publicar no Faceboook.");
				}
			}
		}, callback);
	}
	
	private static void login(final Activity activity, final Runnable tarefa, final Runnable callback) {
		final SharedPreferences preferences = activity.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
		
		boolean autorizado = FACEBOOK.isSessionValid();
		if (!autorizado) {
			String accessToken = preferences.getString("accessToken", null);
			if (accessToken != null) {
				FACEBOOK.setAccessToken(accessToken);
				autorizado = FACEBOOK.isSessionValid();
			}
		}
		
		if (autorizado) {
			executarEmBackgroud(activity, tarefa, callback);
			return;
		}
		
		FACEBOOK.authorize(activity, PERMISSOES, new DialogListener() {
			@Override
			public void onComplete(Bundle values) {
				String accessToken = (String) values.get("access_token");
				Editor preferencesEditor = preferences.edit();
				preferencesEditor.putString("accessToken", accessToken);
				preferencesEditor.commit();
				
				executarEmBackgroud(activity, tarefa, callback);
			}

			@Override
			public void onFacebookError(FacebookError error) {
				Toast.makeText(activity, "Erro ao conectar com o Faceboook.", Toast.LENGTH_LONG).show();
			}

			@Override
			public void onError(DialogError e) {
				Toast.makeText(activity, "Erro ao conectar com o Faceboook.", Toast.LENGTH_LONG).show();
			}

			@Override
			public void onCancel() {
				Toast.makeText(activity, "Conexão com Facebook cancelada.", Toast.LENGTH_LONG).show();
			}
		});
	}

	private static void executarEmBackgroud(final Activity activity, final Runnable tarefa, final Runnable callback) {
		final ProgressDialog progressDialog = ProgressDialog.show(activity, "", "Aguarde...", true);
		new Thread(new Runnable() {
			@Override
			public void run() {
				tarefa.run();
				progressDialog.dismiss();
				callback.run();
			}
		}).start();
	}
}
